package com.boc.langxiang.presenter.shop;

import com.boc.langxiang.contract.shop.ShoppingCarControlContract;
import com.boc.langxiang.entity.request.ShoppingCarDeleteRequest;
import com.boc.langxiang.entity.request.ShoppingCarItemNumberRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.net.HandleErrorSubscriber;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Li Guoxi on 2016/7/1.
 */
public class ShoppingCarControlPresenter extends SuperPresenter implements ShoppingCarControlContract.Presenter {
    ShoppingCarControlContract.View mView;

    public ShoppingCarControlPresenter(ShoppingCarControlContract.View mView) {
        super(mView);
        this.mView = mView;
    }

    @Override
    public void deleteItem(String id) {
        mView.showLoading();
        ShoppingCarDeleteRequest request = new ShoppingCarDeleteRequest();
        request.setId(id);
        mService.deleteShoppingCar(request.params())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.hideLoading();
                        mView.onSuccess();
                    }
                });
    }

    @Override
    public void addNumber(String id, String num) {
        mView.showLoading();
        ShoppingCarItemNumberRequest request = new ShoppingCarItemNumberRequest();
        request.setId(id);
        request.setNumber(num);
        mService.shoppingCarAddNumber(request.params())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.hideLoading();
                        mView.onSuccess();
                    }
                });
    }

    @Override
    public void minusNumber(String id, String num) {
        mView.showLoading();
        ShoppingCarItemNumberRequest request = new ShoppingCarItemNumberRequest();
        request.setId(id);
        request.setNumber(num);
        mService.shoppingCarMinusNumber(request.params())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.hideLoading();
                        mView.onSuccess();
                    }
                });
    }

    @Override
    public void updateNumber(String id, String num) {
        mView.showLoading();
        ShoppingCarItemNumberRequest request = new ShoppingCarItemNumberRequest();
        request.setId(id);
        request.setNumber(num);
        mService.updateShoppingCarNumber(request.params())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.hideLoading();
                        mView.onSuccess();
                    }
                });
    }
}
